$(function () {
    $("#new").val("");
    $("#agin").val("");
    //实时监控输入框
    inputChange("#new","#newPsdError1");
    inputChange("#agin","#newPsdError2");
    inputChange("#new","#deferentError");
    inputChange("#agin","#deferentError");
    getUrl("retrievePassword/verifyPhone");
    ajaxGetUserForResetPwd();
    function newPsdMessage1(){
        var pass = $("#new").val();
        if(pass==null||pass==""){
            validateSpanMsg("newPsdError1","请输入新密码");
            return false;
        }else if(!(isPassword(pass))){
            validateSpanMsg("newPsdError1","密码格式不正确");
            return false;
        }else{
            return true;
        }
    }
    function newPsdMessage2(){
        var pass = $("#new").val();
        var agin = $("#agin").val();
        if (agin == null || agin == "") {
            validateSpanMsg("newPsdError2","请确认新密码");
            return false;
        }else if(agin != pass){
            validateSpanMsg("deferentError","两次输入的密码不一致");
            return false;
        }else{
            return true;
        }
    }
    $("#new").blur(function(){
        newPsdMessage1();
    })
    $("#agin").blur(function(){
        newPsdMessage2();
    })
    $("#accomplish").click(function () {
        var pass = $("#new").val();
        var agin = $("#agin").val();
        //if(pass==null||pass==""){
        //    validateSpanMsg("newPsdError1","请输入新密码");
        //}else if(agin==null||agin==""){
        //    validateSpanMsg("newPsdError2","请确认新密码");
        //}else if(!isPassword(pass)||!isPassword(agin)){
        //    validateSpanMsg("deferentError","密码格式有误");
        //}else if (isPassword(pass) && isPassword(agin)) {
        //    if (pass == agin) {
        //        ajaxResetPassword(pass);
        //    } else {
        //        validateSpanMsg("deferentError","两次输入的密码不一致！");
        //    }
        //}
        if(newPsdMessage1()&&newPsdMessage2()){
            ajaxResetPassword(pass);
        }
    });
});

/**
 * 获取用户绑定手机
 */
function ajaxGetUserForResetPwd() {
    var url = "/Lvscore_User_M/ajaxGetUserForResetPwd";
    $.post(url, function (data) {
        var text = data.name + "（" + data.mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') + "）";
        $("#user_account").text(text);
    }, "json");
}

/**
 * 修改用户密码
 * @param password    用户输入的新密码
 */
function ajaxResetPassword(password) {
    var url = "/Lvscore_User_M/ajaxResetPassword";
    $.post(
        url,
        {'password': password},
        function (data) {
            if (data == 1) {
                var cookie=new sweetCookie();
                cookie.setCookie("remember","",-1);
                var text1=$("#user_account").text();
                cookie.setCookie("accounterNo",text1);
                window.location.href = "/Lvscore_User_M/userWeb/views/retrievePassword/findSucceedPhone.html"
            } else {
                validateSpanMsg("deferentError","服务器异常，请稍后重试！")
            }
        })
}